package model
{
	import flash.events.EventDispatcher;
		
	public class Wardrobe extends EventDispatcher
	{
	   	private var _clothes:Array = new Array();
	    private var _outfits:Array = new Array();
		
	    ////////////////////////////////////////////////
	    // Public methods
	    
		public function Wardrobe() {
		}
		
		public function get clothes():Array { return _clothes; }
		public function get outfits():Array { return _outfits; }
		
		public function containsCloth(c:Cloth):Boolean {
			for(var i:Number=0;i<_clothes.length;i++){
				var cloth:Cloth = _clothes[i];
				if (cloth == c) {
					return true;
				}
			}
			return false;
		}
		
		public function containsOutfit(o:Outfit):Boolean {
			for(var i:Number=0;i<_outfits.length;i++){
				var outfit:Outfit = _outfits[i];
				if (outfit == o) {
					return true;
				}
			}
			return false;
		}
		
		public function addCloth(c:Cloth):Boolean {
			if ( this.containsCloth(c) ) {
				return false;
			} else {
				_clothes.push(c);
				dispatchEvent(new CheckingEvent("selectCloth", c));
				return true;
			}
		}
				
		public function addOutfit(o:Outfit):Boolean {
			if ( this.containsOutfit(o) ) {
				return false;
			} else {
				_outfits.push(o);
				return true;
			}
		}
		
		public function removeCloth(c:Cloth):Boolean {
			if ( this.containsCloth(c) ) {
				_clothes.splice(_clothes.indexOf(c), 1);
				dispatchEvent(new CheckingEvent("deselectCloth", c));
				return true;
			} else {
				return false;
			}
		}
		
		public function removeOutfit(o:Outfit):Boolean {
			if ( this.containsOutfit(o) ) {
				_outfits.splice(_outfits.indexOf(o), 1);
				return true;
			} else {
				return false;
			}
		}
		
		public function saveWardrobe():void {
			
		}
		
		public function findOutfitsInStyle(style:String):Array {
			var results:Array = new Array();
			for (var i:Number=0; i<_outfits.length; i++){
				if ((outfits[i] as Outfit).style == style) {
					results.push(_outfits[i]);
				}
			}
			return results;
		}
		
		public function findOutfitsInWardrobeByStyle(style:String):Array {
			var outfits:Array = new Array();
			var noOfOutfits:int = Math.floor(Math.random()*3) + 6;
			for(var i:Number=0;i<noOfOutfits;i++){
				var outfit:Outfit = createRandomOutfitForStyle(style);
				if (outfit != null) {
					if (!(this.generatedOutfitsContainsOutfitClone(outfits, outfit))) {
						outfits.push(outfit);
					}
				}
			}
			return outfits;
		}
		
		public function generatedOutfitsContainsOutfitClone(outfits:Array, o:Outfit):Boolean {
			for(var i:Number=0;i<outfits.length;i++){
				var outfit:Outfit = outfits[i];
				if (outfit.isCloneOf(o)) {
					return true;
				}
			}
			return false;
		}
		
		public function createRandomOutfitForStyle(s:String):Outfit {
			var outfit:Outfit = new Outfit(s);
			for(var i:Number=0 ; i<Cloth.clothTypes.length ; i++) {
				var cloth:Cloth = getRandomClothOfType(Cloth.clothTypes[i]);
				if (cloth != null) {
					outfit.addCloth(cloth);
				}
			}
			
			if (outfit.clothes.length == 0) {
				return null;
			} else {
				return outfit;
			}
		}
		
		public function getAllClothesOfType(t:String):Array {
			var results:Array = [];
			for(var i:Number=0;i<_clothes.length;i++){
				var cloth:Cloth = _clothes[i];
				if (cloth.type == t) {
					results.push(cloth);
				}
			}
			return results;
		}
		
		public function getRandomClothOfType(t:String):Cloth {
			var results:Array = getAllClothesOfType(t);
			
			if ((t == 'hat') || (t == 'jacket')) {
				if (Math.random() < 0.5) {
					return null;
				}
			}
			
			if (results.length == 0) {
				return null;
			}
			
			var ind:int = Math.floor(Math.random() * results.length);
			return results[ind];
		}
		
		public function removeSavedOutfitsRelatedToCloth(cloth:Cloth):void {
			var aa:Array = getSavedOutfitsRelatedToCloth(cloth);
			for (var i:Number=0; i<aa.length; i++){
				var o:Outfit = (aa[i] as Outfit);
				this.removeOutfit(o);
			}
		}
		
		public function getSavedOutfitsRelatedToCloth(cloth:Cloth):Array {
			var savedOutfits:Array = new Array();
			for (var i:Number=0; i<_outfits.length; i++){
				var o:Outfit = (outfits[i] as Outfit);
				if (o.contains(cloth)) {
					savedOutfits.push(o);
				}
			}
			return savedOutfits;
		}
	}
}